package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.entity.user.Member;
import cn.livingCloud.repository.MemberMapper;
import cn.livingCloud.wechat.protocol.WechatUserInfo;
import cn.livingCloud.wechat.protocol.req.UnSubscribePushMsg;

import java.util.Date;

/**
 * Created by sujiantang on 2015/12/29.
 */
public class MemberUpdater {
    private transient MemberMapper mapper = BeanProvider.getBean(MemberMapper.class);

    public MemberUpdater() {
    }

    public Member create(WechatUserInfo userInfo) {
        Member member = new Member(userInfo)
                .setState(true)
                .setCreateTime(new Date());
        mapper.save(member);
        return member;
    }

    public void updateUnSubscribe(UnSubscribePushMsg unSubscribePushMsg) {
        Member member = mapper.findMemberByOpenid(unSubscribePushMsg.getFromUserName());
        if (member != null && member.isState()) {
//            member.setState(false);
            mapper.update(member);
        }
    }
}
